package com.moli.iov.base.remote.impl;

import com.moli.iov.base.cache.redis.VehicleFollowGroupCacheService;
import com.moli.iov.base.enums.YesOrNoEnum;
import com.moli.iov.base.model.dto.req.UpdateVehicleAttentionReqDto;
import com.moli.iov.base.model.dto.req.VehicleAttentionReqDto;
import com.moli.iov.base.model.dto.req.VehicleFollowGroupReqDto;
import com.moli.iov.base.model.po.VehicleAttention;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.model.po.VehicleFollowGroup;
import com.moli.iov.base.remote.IVehicleAttentionRemoteService;
import com.moli.iov.base.remote.IVehicleFollowGroupRemoteService;
import com.moli.iov.base.service.IVehicleAttentionService;
import com.moli.iov.base.service.IVehicleBaseService;
import com.moli.iov.base.service.IVehicleFollowGroupService;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.JsonUtils;
import com.moli.iov.util.ValidationUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 车辆关注业务处理
 *
 * @author whl
 */
@Service(group = "${service.group}")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class IVehicleFollowGroupRemoteServiceImpl implements IVehicleFollowGroupRemoteService {

  @Autowired
  private IVehicleBaseService iVehicleBaseService;

  @Autowired
  private IVehicleFollowGroupService iVehicleFollowGroupService;

  @Autowired
  private IVehicleAttentionService iVehicleAttentionService;

  @Autowired
  VehicleFollowGroupCacheService vehicleFollowGroupCacheService;

  /**
   * According to pass vehicle follow group request data transfer object for persist a vehicle follow group.
   * @param vehicleFollowGroupReqDto Vehicle follow group request data transfer object
   * @return Restful for response result.
   */
  @Override
  public RestResponse save(VehicleFollowGroupReqDto vehicleFollowGroupReqDto){
    log.info("车辆监控 - 车辆管理-关注组-添加-入参VehicleAttentionReqDto:{}", JsonUtils.toJson(vehicleFollowGroupReqDto));
    if (vehicleFollowGroupReqDto == null) {
      return RestResponse.failure("添加车辆关注参数为空");
    }

    RestResponse restResponse = iVehicleFollowGroupService.save(vehicleFollowGroupReqDto);
    log.info("车辆监控 - 车辆管理-关注组-添加-出参RestResponse:{}", JsonUtils.toJson(restResponse));
    return restResponse;
  }

  /**
   * According to pass vehicle follow group request data transfer object for modified a vehicle follow group.
   * @param vehicleFollowGroupReqDto Vehicle follow group request data transfer object
   * @return Restful for response result.
   */
  @Deprecated
  @Override
  public RestResponse modified(UpdateVehicleAttentionReqDto vehicleFollowGroupReqDto){
      log.info("车辆监控 - 车辆管理-关注组更新-入参UpdateVehicleAttentionReqDto:{}", JsonUtils.toJson(vehicleFollowGroupReqDto));
      if (vehicleFollowGroupReqDto == null) {
        return  RestResponse.failure("车辆关注组参数为空");
      }
      VehicleFollowGroup vehicleFollowGroup = iVehicleFollowGroupService.getById(vehicleFollowGroupReqDto.getGroupId());
      if (vehicleFollowGroup == null) {
        return RestResponse.failure("车辆关注组不存在");
      }
      vehicleFollowGroup.setIsDelete(YesOrNoEnum.YES.getCode());
      vehicleFollowGroup.setGmtModified(new Date());
      VehicleFollowGroupReqDto dto = new VehicleFollowGroupReqDto();
      CommonUtils.copyProperties(vehicleFollowGroup, dto);
      RestResponse restResponse = iVehicleFollowGroupService.saveOrUpdate(dto);
      log.info("车辆监控 - 车辆管理-关注组更新-出参RestResponse:{}", JsonUtils.toJson(restResponse));
      return  restResponse;
  }

  /**
   * According to pass vehicle follow group request data transfer object for batch processor persist a vehicle follow group.
   * @param vehicleFollowGroupReqDto Vehicle follow group request data transfer object
   * @return Restful for response result.
   */
  @Override
  public RestResponse batch(VehicleFollowGroupReqDto vehicleFollowGroupReqDto){
      log.info("车辆监控 - 车辆管理-关注组添加-入参VehicleFollowGroupReqDto:{}", JsonUtils.toJson(vehicleFollowGroupReqDto));
      ValidationUtils.validateBean(vehicleFollowGroupReqDto, VehicleFollowGroupReqDto.Save.class);
      List<VehicleAttention> list = new ArrayList<>();
      if (CollectionUtils.isEmpty(vehicleFollowGroupReqDto.getGroupName())) {
        throw new BusinessException("参数为空");
      }
      RestResponse success = iVehicleFollowGroupService.save(vehicleFollowGroupReqDto);
      log.info("车辆监控 - 车辆管理-关注组添加-出参VehicleFollowGroupReqDto:{}", JsonUtils.toJson(success));
      return success;
  }

  /**
   * According to pass vehicle follow group request data transfer object for delete a vehicle follow group.
   * @param vehicleFollowGroupReqDto Vehicle follow group request data transfer object
   * @return Restful for response result.
   */
  @Override
  public RestResponse delete(VehicleFollowGroupReqDto vehicleFollowGroupReqDto){
    log.info("车辆监控 - 车辆管理-关注组、关注车辆批量删除-入参VehicleFollowGroupReqDto:{}", JsonUtils.toJson(vehicleFollowGroupReqDto));
    ValidationUtils.validateBean(vehicleFollowGroupReqDto, VehicleAttentionReqDto.Delete.class);

  boolean isSuccess = iVehicleFollowGroupService.removeById(vehicleFollowGroupReqDto.getId());
  if(isSuccess){
      String groupId = vehicleFollowGroupReqDto.getId().toString();
      vehicleFollowGroupCacheService.deleteField(vehicleFollowGroupReqDto.getUserId(),groupId);
      iVehicleAttentionService.removeByUserIdAndGroupId(vehicleFollowGroupReqDto.getSysUserId(), vehicleFollowGroupReqDto.getId());
  }

    if(isSuccess){
      return RestResponse.success();
    }else{
      return RestResponse.failure("车辆监控 - 车辆管理-关注组、关注车辆批量删除发生错误");
    }
  }

}
